package com.ohs.fo.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ohs.bll.service.LocationService;
import com.ohs.bll.service.RoomService;
import com.ohs.bll.service.ScheduleService;
import com.ohs.bll.util.Constant;
import com.ohs.bo.form.LoginUser;
import com.ohs.dal.model.Location;
import com.ohs.dal.model.Room;
import com.ohs.dal.model.Schedule;

public class UserScheduleAction extends AbstractAction {

	private static String[] dayNames = {"Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy"};
	private RoomService roomService;
	private ScheduleService scheduleService;
	private LocationService locationService;
	private String roomName;
	private String roomInfo;
	private Location location;
	private List<Room> rooms;
	private List<Schedule> scheduleList;
	private List<String> workingHours;
	private List<String> workingMins;
	private Long roomId;
	private Integer timeInterval;
	private Room room;
	private List<String> workingDay;
	private List<String> startHour;
	private List<String> endHour;
	private List<String> startMin;
	private List<String> endMin;
	private String startBreakHour;
	private String startBreakMin;
	private String endBreakHour;
	private String endBreakMin;
	private boolean needCompleteProfile;
	private Integer selectBefore;
	private Integer allowBookBefore;
	private Integer comeBefore;
	private String turnOn;

	public String setupSchedule() throws Exception {
		clearErrorsAndMessages();
		location = locationService.getByAccount(getUsername());
		selectBefore = (location.isAllowBookRightaway()) ? 1 : 0;
		allowBookBefore = location.getAllowBookBefore();
		comeBefore = location.getComeBefore();
		return "success";
	}

	public String submitSchedule() throws Exception {
		clearErrorsAndMessages();
		location = locationService.getByAccount(getUsername());
		location.setAllowBookBefore(allowBookBefore);
		boolean allow = (selectBefore == 1) ? true : false;
		location.setAllowBookRightaway(allow);
		location.setComeBefore(comeBefore);
		locationService.update(location);
		addActionMessage("Cập nhật thành công");
		return "success";
	}
	
	public String setupRoom() throws Exception {
		location = locationService.getByAccount(getUsername());
		rooms = roomService.findByLocation(location.getLocationId());
		return "success";
	}

	private String getUsername() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = (String) request.getSession().getAttribute(Constant.USER_NAME);
		return username;
	}
	
	public String updateRoom() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		LoginUser user = (LoginUser) request.getSession().getAttribute(Constant.USER_ACCOUNT);
		if (user.getStep() < Constant.STEP_ROOM_1) {
			needCompleteProfile = true;
		}
		try {
			roomId = Long.valueOf(request.getParameter("roomId"));
		} catch (Exception e) {
			roomId = null;
		}
		roomName = null;
		roomInfo = null;
		scheduleList = null;
		if (roomId != null && roomId > 0) {
			room = roomService.get(roomId);
			scheduleList = scheduleService.findByRoom(roomId);
			timeInterval = room.getTimeslotInterval();
			startBreakHour = room.getStartBreakHour().toString();
			startBreakMin = room.getStartBreakMin().toString();
			endBreakHour = room.getEndBreakHour().toString();
			endBreakMin = room.getEndBreakMin().toString();
		} 
		if (scheduleList == null || scheduleList.size() == 0) {
			scheduleList = new ArrayList<Schedule>();
			for (int i=1; i<=7; i++) {
				scheduleList.add(new Schedule());
			}
		}
		return "success";
	}
	
	public String saveRoom() throws Exception {
		String username = getUsername();
		location = locationService.getByAccount(username);
		
		Room room = new Room();
		room.setName(roomName);
		if (roomInfo != null) {
			room.setInfo(roomInfo);
		}
		room.setLocation(location);
		room.setCreatedDate(new Date());
		room.setTimeslotInterval(timeInterval);
		room.setTurnOn(Boolean.parseBoolean(turnOn));
		if (roomId != null && roomId > 0) {
			room.setRoomId(roomId);
		}
		if (new Integer(startBreakHour) > 0 && new Integer(endBreakHour) > 0) {
			room.setStartBreakHour(new Integer(startBreakHour));
			room.setStartBreakMin(new Integer(startBreakMin));
			room.setEndBreakHour(new Integer(endBreakHour));
			room.setEndBreakMin(new Integer(endBreakMin));
		} else {
			room.setStartBreakHour(0);
			room.setStartBreakMin(0);
			room.setEndBreakHour(0);
			room.setEndBreakMin(0);
		}
		
		Map<Integer, Schedule> schedules = new HashMap<Integer, Schedule>();
		for (int i=1; i<= 7 ; i++) {
			Schedule schedule;
			if (workingDay.size() > i && workingDay.get(i) != null && workingDay.get(i).equals("true")) {
				schedule = new Schedule(Integer.valueOf(startHour.get(i)), 
					Integer.valueOf(endHour.get(i)), Integer.valueOf(startMin.get(i)), Integer.valueOf(endMin.get(i)));
				schedule.setEnable(true);
			} else {
				schedule = new Schedule();
				schedule.setEnable(false);
			}
			schedule.setCreatedDate(new Date());
			schedule.setUpdateDate(new Date());
			schedules.put(i, schedule);
		}
		
		LoginUser user = (LoginUser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_ACCOUNT);
		if (user.getStep() < Constant.STEP_ROOM_1) {
			roomService.saveInTransaction(room, schedules, true, username);
			user.setStep(Constant.STEP_DONE_3);
		} else {
			roomService.saveInTransaction(room, schedules, false, username);
		}
		
		return "success";
	}

	public String deleteRoom() throws Exception {
		if (roomId != null && roomId > 0) {
			roomService.delete(roomId);
		}
		return null;
	}
	
	public static String getDayName(String pos) {
		return dayNames[Integer.parseInt(pos) - 1];
	}
	
	public static String formatMin(int min) {
		if (min < 10) {
			return "0" + min;
		} else {
			return min + "";
		}
	}
	
	public List<String> getWorkingDay() {
		return workingDay;
	}

	public void setWorkingDay(List<String> workingDay) {
		this.workingDay = workingDay;
	}

	public boolean isNeedCompleteProfile() {
		return needCompleteProfile;
	}

	public void setNeedCompleteProfile(boolean needCompleteProfile) {
		this.needCompleteProfile = needCompleteProfile;
	}

	public Integer getTimeInterval() {
		return timeInterval;
	}

	public void setTimeInterval(Integer timeInterval) {
		this.timeInterval = timeInterval;
	}

	public Integer getSelectBefore() {
		return selectBefore;
	}

	public void setSelectBefore(Integer selectBefore) {
		this.selectBefore = selectBefore;
	}

	public Integer getAllowBookBefore() {
		return allowBookBefore;
	}

	public void setAllowBookBefore(Integer allowBookBefore) {
		this.allowBookBefore = allowBookBefore;
	}

	public Integer getComeBefore() {
		return comeBefore;
	}

	public void setComeBefore(Integer comeBefore) {
		this.comeBefore = comeBefore;
	}

	public List<Schedule> getScheduleList() {
		return scheduleList;
	}

	public void setScheduleList(List<Schedule> scheduleList) {
		this.scheduleList = scheduleList;
	}

	public List<String> getStartHour() {
		return startHour;
	}

	public void setStartHour(List<String> startHour) {
		this.startHour = startHour;
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public RoomService getRoomService() {
		return roomService;
	}

	public void setRoomService(RoomService roomService) {
		this.roomService = roomService;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public String getRoomInfo() {
		return roomInfo;
	}

	public Long getRoomId() {
		return roomId;
	}

	public void setRoomId(Long roomId) {
		this.roomId = roomId;
	}

	public void setRoomInfo(String roomInfo) {
		this.roomInfo = roomInfo;
	}

	public ScheduleService getScheduleService() {
		return scheduleService;
	}

	public void setScheduleService(ScheduleService scheduleService) {
		this.scheduleService = scheduleService;
	}

	public LocationService getLocationService() {
		return locationService;
	}

	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public List<Room> getRooms() {
		return rooms;
	}

	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}

	public List<String> getWorkingHours() {
		workingHours = new ArrayList<String>();
		for (int h=0; h<=23; h++) {
			workingHours.add(h + "");
		}		
		return workingHours;
	}

	public void setWorkingHours(List<String> workingHours) {
		this.workingHours = workingHours;
	}

	public List<String> getWorkingMins() {
		workingMins = new ArrayList<String>();
		for (int m=0; m<=60; m++) {
			if (m < 10) {
				workingMins.add("0" + m);
			} else {
				workingMins.add(m + "");
			}
		}	
		return workingMins;
	}
	
	public List<String> getCheckMins() {
		workingMins = new ArrayList<String>();
		for (int m=1; m<=60; m++) {
			workingMins.add(m + "");
		}	
		return workingMins;
	}

	public void setWorkingMins(List<String> workingMins) {
		this.workingMins = workingMins;
	}

	public List<String> getEndHour() {
		return endHour;
	}

	public void setEndHour(List<String> endHour) {
		this.endHour = endHour;
	}

	public List<String> getStartMin() {
		return startMin;
	}

	public void setStartMin(List<String> startMin) {
		this.startMin = startMin;
	}

	public String getTurnOn() {
		return turnOn;
	}

	public void setTurnOn(String turnOn) {
		this.turnOn = turnOn;
	}

	public List<String> getEndMin() {
		return endMin;
	}

	public void setEndMin(List<String> endMin) {
		this.endMin = endMin;
	}

	public String getStartBreakHour() {
		return startBreakHour;
	}

	public void setStartBreakHour(String startBreakHour) {
		this.startBreakHour = startBreakHour;
	}

	public String getStartBreakMin() {
		return startBreakMin;
	}

	public void setStartBreakMin(String startBreakMin) {
		this.startBreakMin = startBreakMin;
	}

	public String getEndBreakHour() {
		return endBreakHour;
	}

	public void setEndBreakHour(String endBreakHour) {
		this.endBreakHour = endBreakHour;
	}

	public String getEndBreakMin() {
		return endBreakMin;
	}

	public void setEndBreakMin(String endBreakMin) {
		this.endBreakMin = endBreakMin;
	}

	
}